<script type="text/javascript">
    /**
     * 普通上传图片
     */
    layui.use(['form', 'jquery', 'upload','element'], function () {
        $ = layui.jquery;
        var form = layui.form,
            upload = layui.upload,
            element = layui.element;

        element.on('tab(filter)', function(item){
            form.render('select');
            var daIn = $('.product').length;
            if(daIn == 0 && item.index == 1){
                $(".add_specs").trigger("click");
            }
            upload.render();
        });

        //普通图片上传
        upload.render({
            elem: '.upimg'
            , url: 'admin-upload-img'
            , accept: 'images'
            , acceptMime: 'image/jpg, image/png, image/jpeg'
            , size: '1024'
            , done: function (res) {
                //上传成功
                if (res.code == 0) {
                    $('.is_show').attr('src', res.url);
                    $("#img_url").val(res.src);
                }
            }
        });
        //多图片上传
        upload.render({
            elem: '#more_img'
            , url: 'admin-upload-img'
            , acceptMime: 'image/jpg, image/png, image/jpeg'
            , multiple: true
            , size: 1024
            , done: function (res) {
                //上传完毕
                if (res.code == 0) {
                    var imgdta = '<span class="img-list"><img src="' + res.url + '" class="layui-upload-img">' +
                        '<input type="hidden" name="img[]" value="' + res.src + '">' +
                        '<i class="layui-icon close" onclick="$(this).parent().remove();">ဆ</i></span>';
                    $('#imgLst').before(imgdta);
                }
            }
        });

        //多图片上传(编辑)
        upload.render({
            elem: '#edit_more_img'
            , url: 'admin-upload-img'
            , acceptMime: 'image/jpg, image/png, image/jpeg'
            , multiple: true
            , size: 1024
            , done: function (res) {
                //上传完毕
                if (res.code == 0) {
                    var count = $('.imgNum').length;
                    var imgdta = '<span class="img-list"><img src="' + res.url + '" class="layui-upload-img imgNum">' +
                        '<input type="hidden" name="img['+count+'][id]" value="0">' +
                        '<input type="hidden" name="img['+count+'][url]" value="' + res.src + '">' +
                        '<i class="layui-icon close" onclick="$(this).parent().remove();">ဆ</i></span>';
                    $('#imgLst').before(imgdta);
                }
            }
        });


        $('.product').each(function (i) {
            upSpesImg('.specs_fex'+i);
        });
        /*upSpesImg('.specs_fex');*/

        //规格属性图片
        function upSpesImg(elem) {
            //普通图片上传
            upload.render({
                elem: elem
                , url: 'admin-upload-img'
                , accept: 'images'
                , acceptMime: 'image/jpg, image/png, image/jpeg'
                , size: '1024'
                , done: function (res) {
                    //上传成功
                    if (res.code == 0) {
                        $(elem).find('.specs_pic').attr('src', res.url);
                        $(elem).find('.specs_url').val(res.src);
                    }
                }
            });
        }

        //追加规格属性
        $(document).on('click', '.add_specs', function () {
            var goods_type = $("input[name='goods[type]']:checked").val();
            if(goods_type != 2){
                var dis = 'display: none;';
                var req = '';
            }else{
                var dis = '';
                var req = 'required';
            }
            var daIn = $('.product').length+1;
            if (daIn == 1) {
                var xian = 'display: block';
                var shan = 'display: none';
            } else {
                var xian = 'display: none';
                var shan = 'display: block';
            }

            var type = '<div class="layui-form-item">\n' +
                '<div class="layui-inline"style="width: 100%">\n' +
                '<label for="L_alias" class="layui-form-label">\n' +
                '<span class="x-red">*</span>规格属性</label>\n' +
                '<div class="layui-input-inline" style="width:45%">\n' +
                '<input type="text" name="product['+daIn+'][name]" value="" placeholder="规格属性" lay-verify="required" class="layui-input">\n' +
                '</div>\n' +
                '<div class="layui-input-inline" style="width:8%">\n' +
                '<input type="text" name="product['+daIn+'][price]" value="" placeholder="价格" lay-verify="required" class="layui-input">\n' +
                '</div>\n' +
                '<div class="layui-input-inline integral" style="width:8%;'+dis+'">\n' +
                '<input type="text" name="product['+daIn+'][integral]" value="" placeholder="积分" lay-verify="'+req+'" class="layui-input">\n' +
                '</div>\n' +
                '<div class="layui-input-inline" style="width:8%">\n' +
                '<input type="text" name="product['+daIn+'][stock]" value="" placeholder="库存" lay-verify="required" class="layui-input">\n' +
                '</div>\n' +
                '<div class="layui-input-inline specs_fex'+daIn+' product" style="width:50px">\n' +
                '<input type="hidden" name="product['+daIn+'][pic]" value="" lay-verify="goods_pic" class="specs_url">\n' +
                '<img class="specs_pic upspecsimg" src="admin/images/upload.png">\n' +
                '</div>\n' +
                '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm del" style="'+shan+'"><i class="layui-icon"></i></button>'+
                '<button type="button" class="layui-btn layui-btn-primary layui-btn-sm add_specs" da-in="0" style="'+xian+'"><i class="layui-icon"></i></button>'+
                '</div>\n' +
                '</div>';
            $(this).parent().parent().parent().append(type);
            upSpesImg('.specs_fex'+daIn);
        });

        //删除元素
        $(document).on('click', '.del', function () {
            $(this).parent().parent().remove();
            upSpesImg();
        });

        //获取二级分类
        form.on('select(maintain_father)', function (data) {
            $.ajax({
                url: js_url('shop-maintain-list'),
                type: 'get',
                data: {pid: data.value},
                dataType: "json",
                success: function (result) {
                    if(result.state == 0){
                        var option = '<option value="">请选择二级分类</option>';
                        $.each(result.data,function(index,value){
                            option += '<option value="'+value.id+'">'+value.name+'</option>';
                        });
                        $('#maintain_son').html(option);
                    }

                    form.render('select');
                }
            });
        });

    });


    $(document).ready(function () {
        $(".show_img").hover(function () {
            $(".is_show").css("display", "block");
        }, function () {
            $(".is_show").css("display", "none");
        });

    });

    //删除数据
    $(document).on('click', '.delelt', function () {
        var id = $(this).attr('del-id');
        var that = $(this);
        if (id) {
            layer.confirm('确定删除已有数据吗？', {btn: ['确定', '取消']}, function () {
                $.ajax({
                    type: "post",
                    url: "shop-goods-delProduct",
                    data:{id:id},
                    dataType: "json",
                    success: function (obj) {
                        console.log(obj);
                        if (obj.state == 0) {
                            that.parent().parent().remove();
                            layer.msg('删除成功', {time: 2000, icon: 6});
                        } else {
                            layer.msg('删除失败', {time: 2000, icon: 5});
                        }
                    }, error: function () {
                        alert("发生错误：");
                    }
                });
            });
        }
    });

    //删除图片
    $(document).on('click', '.del-img', function () {
        var id = $(this).attr('del-id');
        var that = $(this);
        if (id) {
            layer.confirm('确定删除已有数据吗？', {btn: ['确定', '取消']}, function () {
                $.ajax({
                    type: "post",
                    url: "shop-goods-delPic",
                    data:{id:id},
                    dataType: "json",
                    success: function (obj) {
                        console.log(obj);
                        if (obj.state == 0) {
                            that.parent().remove();
                            layer.msg('删除成功', {time: 2000, icon: 6});
                        } else {
                            layer.msg('删除失败', {time: 2000, icon: 5});
                        }
                    }, error: function () {
                        alert("发生错误：");
                    }
                });
            });
        }
    });


</script>